﻿<cfscript>
/** 
* @hint "Entity: StudentMarkResitTemp, StudentExamResit, ExamResitPlan, ExamResitTask, Course"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.taskResitExamListener" 
	output=false 
	depends="sequenceExamResitTask"
	{
		
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 

	
	/**
	* @hint "清除全校补考的时间、地点安排"
	* 
	* @event "MachII Event Object"
	*/	
	public void function clearExamTask( required MachII.framework.Event event ) output=false {
		
		var termId = event.getArg("TermID");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 默认考试场次 */
										
			sql = "SELECT a.task_exrstid 
					FROM t_resittask a  
					WHERE 
						a.term_id = :termId 
						AND 
						( 
							a.task_prog = '2'
							OR 
							a.task_prog = '1'
						) ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			
			var rs_examTask = queryObj.execute( sql=sql ).getResult();
			
			for (  var a=1; a LTE rs_examTask.recordCount; a++ ) {
				
				var TaskResitExamID = rs_examTask["task_exrstid"][a];
				
				/* 清除考试时间安排 */
			
				/* 更新排考进度 */
		
				sql = "UPDATE t_resittask  a
						SET a.wdidx = '0' ,
							a.cellidx = 0 ,
							a.task_prog = '0' 
						WHERE a.task_exrstid = :TaskResitExamID ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="TaskResitExamID", value=TaskResitExamID, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
				/* 数据密闭 */
				
				/* 如果已经保存了学生考试安排 则全部删除 */
		
				sql = "DELETE 
						FROM t_student_resit a 
						WHERE a.task_exrstid = :TaskResitExamID ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="TaskResitExamID", value=TaskResitExamID, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
				/* 如果已经保存了场地占用 则全部删除 */
			
				sql = "DELETE 
						FROM t_clsroom_timetable a 
						WHERE a.crtime_id IN (
							  SELECT b.crtime_id
							  FROM t_clsroom_forresit b 
							  WHERE b.task_exrstid = :TaskResitExamID 
						)";
							
				queryObj = new Query( datasource=application.dnsMaster ) ;
				queryObj.addParam( name="TaskResitExamID", value=TaskResitExamID, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
			}
				
		}
		
	}
	
	
	/**
	* @hint "向学生发布所有已安排时间及地点补考安排"
	* 
	* @event "MachII Event Object"
	*/	
	public void function publishExamTask( required MachII.framework.Event event ) output=false {
			
		var termId = event.getArg("TermID");
		var sql = "";
		var queryObj = "";
			
		/* Every SQL operation is TRANSACATION*/
		transaction {
	
			var TaskProgress = "3";
			
			sql = "UPDATE t_resittask a 
					SET a.task_prog = : TaskProgress 
					WHERE a.term_id = :termId 
								AND 
								a.task_prog = '2' ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="TaskProgress", value=TaskProgress, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );
					
		}
			
	}
	
	/**
	* @hint "标志课程需参加补考计划安排"
	* 
	* @event "MachII Event Object"
	*/	
	public void function restoreExamCourse( required MachII.framework.Event event ) output=false {
		
		var courseId = event.getArg("CourseID");
		var termId = event.getArg("TermID");
		var taskType = event.getArg("TaskType");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
						
			sql = "SELECT 
					a.task_id 
					FROM t_resitplan a  
					WHERE
						a.cid = :courseId 
						AND 
						a.term_id = :termId 
						AND 
						a.task_type = :taskType 
						AND 
						a.is_arrange = 'N'
					  ORDER BY a.task_id  ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_char" );
			
			var rs_plan = queryObj.execute( sql=sql ).getResult();
			
			for (  var a=1; a LTE rs_plan.recordCount; a++ ) {
		
				var taskId = rs_plan["task_id"][a];
				
				sql = "SELECT a.task_type 
						FROM t_resitplan a 
						WHERE a.task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				
				var rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
				var IsArrange = "0";
				var ExamResitIndex = rs_planEntity.task_type & "00000" ;
				
				sql = "UPDATE t_resitplan a 
						SET a.is_arrange = :IsArrange ,
							a.rstidx = :ExamResitIndex 
						WHERE a.task_id = :taskId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
			}
		}

	}
		
	/**
	* @hint "标志课程不参加补考计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamCourse( required MachII.framework.Event event ) output=false {
		
		var courseId = event.getArg("CourseID");
		var termId = event.getArg("TermID");
		var taskType = event.getArg("TaskType");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
							
			sql = "SELECT 
					a.task_id 
					FROM t_resitplan a  
					WHERE
						a.cid = :courseId 
						AND 
						a.term_id = :termId 
						AND 
						a.task_type = :taskType 
						AND 
						a.is_arrange = '0'
					  ORDER BY a.task_id  ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_char" );
			
			var rs_plan = queryObj.execute( sql=sql ).getResult();

			for (  var a=1; a LTE rs_plan.recordCount; a++ ) {

				var taskId = rs_plan["task_id"][a];
				
				sql = "SELECT a.task_type 
						FROM t_resitplan a 
						WHERE a.task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				
				var rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
				var IsArrange = "N";
				var ExamResitIndex = rs_planEntity.task_type & "00000" ;
				
				sql = "UPDATE t_resitplan a 
						SET a.is_arrange = :IsArrange ,
							a.rstidx = :ExamResitIndex 
						WHERE a.task_id = :taskId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
			}
			
			
		}

	}
	
	/**
	* @hint "标志课程的指定教学班需参加期末考试计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function restoreExamTaskClass( required MachII.framework.Event event ) output=false {

		var taskId = event.getArg("TaskID");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
	
			sql = "SELECT a.task_type 
						FROM t_resitplan a 
						WHERE a.task_id = :taskId ";
				
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			var rs_planEntity = queryObj.execute( sql=sql ).getResult();
			
			var IsArrange = "0";
			var ExamResitIndex = rs_planEntity.task_type & "00000" ;
			
			sql = "UPDATE t_resitplan a 
					SET a.is_arrange = :IsArrange ,
						a.rstidx = :ExamResitIndex 
					WHERE a.task_id = :taskId ";
					
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );
			
		}
		
	}
	
	/**
	* @hint "标志课程的指定教学班不参加补考计划安排"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamTaskClass( required MachII.framework.Event event ) output=false {

		var taskId = event.getArg("TaskID");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
	
			sql = "SELECT a.task_type 
						FROM t_resitplan a 
						WHERE a.task_id = :taskId ";
				
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			var rs_planEntity = queryObj.execute( sql=sql ).getResult();
			
			var IsArrange = "N";
			var ExamResitIndex = rs_planEntity.task_type & "00000" ;
			
			sql = "UPDATE t_resitplan a 
					SET a.is_arrange = :IsArrange ,
						a.rstidx = :ExamResitIndex 
					WHERE a.task_id = :taskId ";
					
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );
			
		}
		
	}
		
	/**
	* @hint "删除补考任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeExamTask( required MachII.framework.Event event ) output=false {
		
		var taskExid = event.getArg("TaskExamID");
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT  a.cid,a.term_id,
						   a.rstidx
					FROM t_resittask a 
					WHERE a.task_exrstid = :taskExid ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="taskExid", value=taskExid, cfsqltype="cf_sql_char" );
			
			var rs_examTask = queryObj.execute( sql=sql ).getResult();

			var courseId = rs_examTask.cid;
			var termId = rs_examTask.term_id;
			var examIdx = rs_examTask.rstidx;
			
			sql = "SELECT 
					a.task_id 
					FROM t_resitplan a  
					WHERE
						a.cid = :courseId 
						AND 
						a.term_id = :termId 
						AND 
						a.rstidx = :examIdx ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examIdx", value=examIdx, cfsqltype="cf_sql_char" );
			
			var rs_plan = queryObj.execute( sql=sql ).getResult();
			
			for (  var a=1; a LTE rs_plan.recordCount; a++ ) {
				
				var taskId = rs_plan["task_id"][a];

				sql = "SELECT a.task_type 
						FROM t_resitplan a 
						WHERE a.task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				
				var rs_planEntity = queryObj.execute( sql=sql ).getResult();
			
				var IsArrange = "0";
				var ExamResitIndex = rs_planEntity.task_type & "00000" ;
				
				sql = "UPDATE t_resitplan a 
						SET a.is_arrange = :IsArrange ,
							a.rstidx = :ExamResitIndex 
						WHERE a.task_id = :taskId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
			}
			
			sql = "DELETE 
					FROM t_resittask a 
					WHERE a.task_exrstid = :taskExid ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="taskExid", value=taskExid, cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );
			
		}
	}
	
	/**
	* @hint "创建补考任务"
	* 
	* @event "MachII Event Object"
	*/
	public void function createExamTask( required MachII.framework.Event event ) output=false {

		var taskIds = event.getArg("TaskClassIDs");
		var examWeek = event.getArg("WID");
		
		var sql = "";
		var queryObj = ""; 
		var sequenceExamResitTask = getsequenceExamResitTask();
				
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var term = event.getArg("TermID");
			var course = event.getArg("CourseID");
			var campus = event.getArg("CampusID");
		
			var cell = 0;
			
			/* 选课且已缴费的学生 */
			var chooserCount = 0;
	
				
			/* 生成考序号 */
			sequenceExamResitTask.setTermID(term);
			var examIdx = sequenceExamResitTask.getExamIDX();
			
			/* 生成考试任务号 */
			sequenceExamResitTask.setExamResitIndex(examIdx);
			var examTaskId =sequenceExamResitTask.getID();
	
			var studentCount = 0;

			/* 标注考试计划未已安排考试任务 */
			for ( var taskId in listToArray(taskIds, "|") ) {
			
				sql = "SELECT a.stu_sum 
						FROM t_resitplan a 
						WHERE a.task_id = :taskId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				
				var rs_planEntity = queryObj.execute( sql=sql ).getResult();
				
				var IsArrange = "1";
				var ExamResitIndex = examIdx;
					
				studentCount = studentCount + rs_planEntity.stu_sum;
					
				sql = "UPDATE t_resitplan a 
						SET a.is_arrange = :IsArrange ,
							a.rstidx = :ExamResitIndex 
						WHERE a.task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
				
			}
			
			var ExamWeek=wid; 
			var ExamWeekDay="0";
			var TaskProgress="0";
			var ExamSize=studentCount;
			var Term=term; 
			var Course=course;
			var ExamCell=cell; 
			var Campus=campus;
			var ExamResitIndex = examIdx;
			var TaskResitExamID = examTaskId;
			
			
			sql = "INSERT INTO t_resittask a (
						   a.rstidx,
						   a.task_exrstid,
						   a.wid,
						   a.wdidx,
						   a.task_prog,
						   a.examer_sum,
						   a.term_id,
						   a.cid,
						   a.cellidx,
						   a.campus_id
					) VALUES (
							:ExamResitIndex ,
							:TaskResitExamID ,
							:ExamWeek ,
							:ExamWeekDay ,
							:TaskProgress ,
							:ExamSize ,
							:Term ,
							:Course ,
							:ExamCell ,
							:Campus 
					)";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TaskResitExamID", value=TaskResitExamID, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="ExamWeek", value=ExamWeek, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="ExamWeekDay", value=ExamWeekDay, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TaskProgress", value=TaskProgress, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="ExamSize", value=ExamSize, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="Term", value=Term, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="Course", value=Course, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="ExamCell", value=ExamCell, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="Campus", value=Campus, cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );
			
			}
	}
	
	/**
	* @hint "清零指定学期补考数据"
	* 
	* @event "MachII Event Object"
	*/	
	public void function clearExamResitCourse( required MachII.framework.Event event ) output=false {
		
		var termId = event.getArg("resitTerm"); /* 补考执行学期 */
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 第一步 清除补考时间地点安排 */
											
			sql = "SELECT a.task_exrstid 
					FROM t_resittask a  
					WHERE 
						a.term_id = :termId ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			
			var rs_examTask = queryObj.execute( sql=sql ).getResult();
			
			for ( var a=1; a LTE rs_examTask.recordCount; a++ ) {

				/* 如果已经保存了学生考试安排 则全部删除 */
		
				var taskExrstId = rs_examTask["task_exrstid"][a];
				
				sql = "DELETE 
						FROM t_student_resit a 
						WHERE a.task_exrstid = :taskExrstId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskExrstId", value=taskExrstId, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
				/* 如果已经保存了场地占用 则全部删除 */
		
				sql = "DELETE 
						FROM 
						t_clsroom_timetable a
						WHERE a.crtime_id IN (
							  SELECT b.crtime_id
							  FROM t_clsroom_forresit b
							  WHERE b.task_exrstid = :taskExrstId )";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskExrstId", value=taskExrstId, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
				
				/* 删除补考任务 */
				sql = "DELETE
						FROM t_resittask a 
						WHERE a.task_exrstid = :taskExrstId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskExrstId", value=taskExrstId, cfsqltype="cf_sql_char" );
				queryObj.execute( sql=sql );
			}
			
			/* 第二步 清除补考计划 */
		
			sql = "SELECT a.task_id 
					FROM t_resitplan a 
					WHERE a.term_id = :termId ";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			
			var rs_examPlan = queryObj.execute( sql=sql ).getResult();
			
			for ( var a=1; a LTE rs_examPlan.recordCount; a++ ) {
				
				/* 删除临时成绩提交记录 */
			
				var ExamPlanId = rs_examPlan["task_id"][a];
				
				sql = "DELETE 
						FROM t_temp_mark_resit a 
						WHERE a.task_id = :ExamPlanId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="ExamPlanId", value=ExamPlanId, cfsqltype="cf_sql_varchar" );
				queryObj.execute( sql=sql );
				
				/* 删除补考计划 */
				sql = "DELETE 
						FROM t_resitplan a 
						WHERE a.task_id = :ExamPlanId ";
						
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="ExamPlanId", value=ExamPlanId, cfsqltype="cf_sql_varchar" );
				queryObj.execute( sql=sql );
				
			}

		}

	}

	/**
	* @hint "根据补考策略, 生成学期补考名单"
	* 
	* @event "MachII Event Object"
	*/	
	public void function generatorExamResitCourse( required MachII.framework.Event event ) output=false {
			
		var termId = event.getArg("markTerm"); /* 课程考试学期 */
		var termIdRST = event.getArg("resitTerm"); /* 补考执行学期 */

		var sql = "";
		var queryObj = "";
		var sequenceExamResitTask = getsequenceExamResitTask();
	
		
			
		/* Every SQL operation is TRANSACATION*/
		transaction {
				
			
		
			var termRST =termIdRST;
			var cell = 0;
				
			/* 第一步 读取需参加补考的成绩提交任务 */
		
			sql = "SELECT 
					a.task_id, a.task_type, 
					a.cid, a.cidx
					FROM t_task_mark a 
						INNER JOIN t_course d ON d.cid = a.cid 
					WHERE 
						a.term_id = :termId 
						AND 
						a.task_prog = '3' 
						AND 
						d.resit_policy = '1' 
						AND 
						NOT EXISTS (
							SELECT 1
							FROM t_resitplan b 
								WHERE 
									a.task_id = b.task_id
									AND 
									b.term_id = :termIdRST 
								) 
						AND 
						EXISTS (
								SELECT 1
								FROM t_student_mark c 
									INNER JOIN t_course e ON e.cid = c.cid
								WHERE 
									(
										c.term_id = :termId 
										AND 
										a.task_id = c.task_id 
										AND 
										e.resit_policy = '1' 
									) 
									AND 
									c.mark_result < 60
										
								) 
					ORDER BY 
						a.cid, a.cidx";
						
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="termIdRST", value=termIdRST, cfsqltype="cf_sql_char" );
			
			var rs_task = queryObj.execute( sql=sql ).getResult();
				
			for ( var a=1; a LTE rs_task.recordCount; a++ ) {
			
				var TaskID = rs_task["task_id"][a];
				var ExamResitIndex = rs_task["task_type"][a] & "00000"; 
				var CourseIndex = rs_task["cidx"][a]; 
				var TaskType = rs_task["task_type"][a]; 
				var IsArrange = "0"; 
				var StudentCount = 0; 
				var TaskProgress = "0"; 
				var MarkDone = 0;
				var MarkNeed = 0; 
				var Term = termRST;
				var Course =rs_task["cid"][a];
				
				sql = "SELECT 1 
						FROM t_resitplan a 
						WHERE a.task_id = :TaskID"; 
						
				queryObj = new Query( datasource=application.dnsMaster);
				
				queryObj.addParam( name="TaskID", value=TaskID, cfsqltype="cf_sql_varchar" );
				
				var rs_check = queryObj.execute( sql=sql ).getResult();
				
				if( rs_check.recordCount EQ 0 ){
					
					sql = "INSERT INTO t_resitplan (
							   task_id,
							   rstidx,
							   cidx,
							   task_type,
							   is_arrange,
							   stu_sum,
							   task_prog,
							   mark_done,
							   mark_need,
							   term_id,
							   cid 
						) VALUES (
							    :TaskID ,
								:ExamResitIndex , 
								:CourseIndex , 
								:TaskType , 
								:IsArrange , 
								:StudentCount , 
								:TaskProgress , 
								:MarkDone ,
								:MarkNeed , 
								:Term ,
								:Course 
						)";
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam( name="TaskID", value=TaskID, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="CourseIndex", value=CourseIndex, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="TaskType", value=TaskType, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="StudentCount", value=StudentCount, cfsqltype="cf_sql_interger" );
					queryObj.addParam( name="TaskProgress", value=TaskProgress, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="MarkDone", value=MarkDone, cfsqltype="cf_sql_interger" );
					queryObj.addParam( name="MarkNeed", value=MarkNeed, cfsqltype="cf_sql_interger" );
					queryObj.addParam( name="Term", value=Term, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="Course", value=Course, cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql );
					
				}
				else{
					sql = "UPDATE 
								t_resitplan 
							SET    
							   rstidx = :ExamResitIndex ,
							   cidx = :CourseIndex ,
							   task_type = :TaskType ,
							   is_arrange = :IsArrange ,
							   stu_sum = :StudentCount ,
							   task_prog = :TaskProgress ,
							   mark_done = :MarkDone ,
							   mark_need = :MarkNeed ,
							   term_id = :Term ,
							   cid = :Course 
							WHERE 
								task_id = :TaskID ";

					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam( name="TaskID", value=TaskID, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="ExamResitIndex", value=ExamResitIndex, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="CourseIndex", value=CourseIndex, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="TaskType", value=TaskType, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="IsArrange", value=IsArrange, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="StudentCount", value=StudentCount, cfsqltype="cf_sql_interger" );
					queryObj.addParam( name="TaskProgress", value=TaskProgress, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="MarkDone", value=MarkDone, cfsqltype="cf_sql_interger" );
					queryObj.addParam( name="MarkNeed", value=MarkNeed, cfsqltype="cf_sql_interger" );
					queryObj.addParam( name="Term", value=Term, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="Course", value=Course, cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql );	
				}
						
				
				
					
			}

			/* 第二步 生成补考学生名单 */
			
			sql = "SELECT 
						c.scc_id, c.task_type, 
						c.retaking, c.mark_mode,
						c.stu_id, c.cid, 
						c.task_id, c.mark_note 
					FROM t_student_mark c 
						INNER JOIN t_course e ON e.cid = c.cid 
					WHERE 
						(
							c.term_id = :termId 
							AND 
							e.resit_policy = '1' 
						) 
						AND 
						EXISTS (
							SELECT 1 
							FROM t_resitplan b 
								WHERE 
									b.task_id = c.task_id
								) 
						AND 
						c.mark_result < 60
						
					ORDER BY 
						c.stu_id, c.cid";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			
			var rs_mark = queryObj.execute( sql=sql ).getResult();
			
			
			for ( var a=1; a LTE rs_mark.recordCount; a++ ) {
				
				var ChooseID = rs_mark["scc_id"][a]; 
				var TaskType = rs_mark["task_type"][a]; 
				var Retaking = rs_mark["retaking"][a]; 
				var MarkMode = rs_mark["mark_mode"][a]; 
				var MarkLimite = 100; 
				var MarkResit = -1; 
				var MarkResitNote = "1"; 
				var Student = rs_mark["stu_id"][a]; 
				var Course = rs_mark["cid"][a]; 
				var Term = termRST; 
				var ExamResitPlan = rs_mark["task_id"][a];


				sql = "SELECT 1 
						FROM t_temp_mark_resit a 
						WHERE a.scc_id = :ChooseID ";
										
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="ChooseID", value=ChooseID, cfsqltype="cf_sql_varchar" );
				
				var rs_checkTemp = queryObj.execute( sql=sql ).getResult();
				
				if( rs_checkTemp.recordCount EQ 0 ) {
					sql = "INSERT INTO t_temp_mark_resit (
							   scc_id,
							   task_type,
							   retaking,
							   mark_mode,
							   mark_limite,
							   mark_resit,
							   mark_renote,
							   stu_id,
							   cid,
							   term_id,
							   task_id
						) VALUES (
								:ChooseID , 
								:TaskType , 
								:Retaking , 
								:MarkMode , 
								:MarkLimite , 
								:MarkResit ,
								:MarkResitNote , 
								:Student , 
								:Course , 
								:Term ,
								:ExamResitPlan 
						)";
					queryObj = new Query( datasource=application.dnsMaster);
					queryObj.addParam( name="ChooseID", value=ChooseID, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="TaskType", value=TaskType, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="Retaking", value=Retaking, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="MarkMode", value=MarkMode, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="MarkLimite", value=MarkLimite, cfsqltype="cf_sql_decimal",scale = 2);
					queryObj.addParam( name="MarkResit", value=MarkResit, cfsqltype="cf_sql_decimal",scale = 2);
					queryObj.addParam( name="MarkResitNote", value=MarkResitNote, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="Student", value=Student, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="Course", value=Course, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="Term", value=Term, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="ExamResitPlan", value=ExamResitPlan, cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql );
				}else{
					sql = "UPDATE t_temp_mark_resit 
							SET  
							   task_type = :TaskType , 
							   retaking = :Retaking , 
							   mark_mode = :MarkMode ,
							   mark_limite = :MarkLimite ,
							   mark_resit = :MarkResit ,
							   mark_renote = :MarkResitNote , 
							   stu_id = :Student , 
							   cid = :Course , 
							   term_id = :Term ,
							   task_id = :ExamResitPlan 
							WHERE 
								scc_id = :ChooseID  
						";
					queryObj = new Query( datasource=application.dnsMaster);
					queryObj.addParam( name="ChooseID", value=ChooseID, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="TaskType", value=TaskType, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="Retaking", value=Retaking, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="MarkMode", value=MarkMode, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="MarkLimite", value=MarkLimite, cfsqltype="cf_sql_decimal",scale = 2);
					queryObj.addParam( name="MarkResit", value=MarkResit, cfsqltype="cf_sql_decimal",scale = 2);
					queryObj.addParam( name="MarkResitNote", value=MarkResitNote, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="Student", value=Student, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="Course", value=Course, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="Term", value=Term, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="ExamResitPlan", value=ExamResitPlan, cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql );
				}
				
			}

			/* 第三步 清理补考学生名单 */
			
			sql = "DELETE 
					FROM t_temp_mark_resit a 
					WHERE 
					a.Term_Id = :termIdRST 
					AND 
					NOT EXISTS (
						SELECT 1 
						FROM t_student_mark c 
							INNER JOIN t_course e ON e.cid = c.cid 
						WHERE 
							a.stu_id = c.stu_id 
							AND 
							a.scc_id = c.scc_id 
							AND 
							(
								(
									c.term_id = :termId 
									AND 
									e.resit_policy = '1' 
								)
								AND 
								c.mark_result < 60
									
							 )
					) ";
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="termIdRST", value=termIdRST, cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );
			
			/* 第四步 清理补考任务 */
			
			sql = "DELETE 
					FROM t_resitplan b 
					WHERE 
					b.term_id = :termIdRST 
					AND 
					NOT EXISTS (
						SELECT 1
						FROM t_student_mark c 
							INNER JOIN t_course e ON e.cid = c.cid
						WHERE 
							(
								c.term_id = :termId 
								AND 
								b.task_id = c.task_id 
								AND 
								e.resit_policy = '1' 
							) 
							AND 
							c.mark_result < 60
								
					)";	
					
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="termIdRST", value=termIdRST, cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );
			/* 第五步 计算补考人数 */
				
			sql = "SELECT 
						a.task_id, 
						count(*) sum
					FROM t_temp_mark_resit a 
					WHERE 
						a.term_id = :termIdRST 
					GROUP BY a.task_id";
			
			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="termIdRST", value=termIdRST, cfsqltype="cf_sql_char" );
			
			var rs_resiter = queryObj.execute( sql=sql ).getResult();
			
			for ( var a=1; a LTE rs_resiter.recordCount; a++ ) {
				
				var taskId = rs_resiter["task_id"][a];
				var StudentCount = 	rs_resiter["sum"][a];
				var MarkNeed = rs_resiter["sum"][a];
				
				sql = "UPDATE t_resitplan a 
						SET a.stu_sum = :StudentCount ,
							a.mark_need = :MarkNeed 
						WHERE a.task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="StudentCount", value=StudentCount, cfsqltype="cf_sql_interger" );
				queryObj.addParam( name="MarkNeed", value=MarkNeed, cfsqltype="cf_sql_interger" );
				queryObj.execute( sql=sql );
				
			}
			
				
		}

			
	}
		
		
		
}
	
</cfscript>